﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using StarsAdmin.Application.LoginLog.Dtos;
using StarsAdmin.Application.OperationLog.Dtos;
using StarsAdmin.Core.App;
using StarsAdmin.Core.Cache;
using StarsAdmin.Core.Consts;
using StarsAdmin.Core.DB.Entities;
using StarsAdmin.Core.Exceptions;
using StarsAdmin.Core.Extensions;
using StarsAdmin.Core.Models;
using StarsAdmin.Repository.LoginLog;
using StarsAdmin.Repository.OperationLog;
using StarsAdmin.Repository.User;

namespace StarsAdmin.Application.LoginLog.Services
{
    public class LoginLogService : ILoginLogService
    {
        private readonly ILoginLogRepository _loginLogRepository;
        private readonly IUserRepository _userRepository;
        private readonly IMapper _mapper;
        private readonly ICacheService _cache;

        public LoginLogService(IMapper mapper, ICacheService cacheService, ILoginLogRepository loginLogRepository, IUserRepository userRepository)
        {
            _loginLogRepository = loginLogRepository;
            _userRepository = userRepository;
            _mapper = mapper;
            _cache = cacheService;
        }

        #region 查询

        public async Task<List<LoginLogDto>> GetAllAsync(BaseQueryInput input)
        {
            var result = await _loginLogRepository.GetAll().AsNoTracking()
           .WhereIf(input != null && input.QueryConditions.Any(), input.QueryConditions)
           .OrderByIf(input != null && input.Sort != null, input.Sort, input.Order != false)
           .ToListAsync();
            return _mapper.Map<List<LoginLogDto>>(result);
        }

        public async Task<Page<LoginLogDto>> GetPageAsync(PageQueryInput input)
        {
            var result = await _loginLogRepository.GetAll().AsNoTracking()
                .WhereIf(input != null && input.QueryConditions.Any(), input.QueryConditions)
                .OrderByIf(input != null && input.Sort != null, input.Sort, input.Order != false)
                .ToPageListAsync(input.CurrentPage, input.PageSize);
            return _mapper.Map<Page<LoginLogDto>>(result);
        }

        public async Task<LoginLogDto?> GetByIdAsync(long id)
        {
            var result = await _loginLogRepository.GetByIdAsync(id);
            return _mapper.Map<LoginLogDto>(result);
        }

        public async Task<List<LoginLogDto>> GetByIdsAsync(long[] ids)
        {
            var result = await _loginLogRepository.GetByIds(ids).ToListAsync();
            return _mapper.Map<List<LoginLogDto>>(result);
        }

        #endregion 查询

        #region 添加

        public async Task<bool> AddAsync(AddLoginLogInput input)
        {
            var user = await _userRepository.GetBy(e => e.UserName == input.UserName).FirstOrDefaultAsync();
            if (user == null)
            {
                throw new NotFoundException("当前用户不存在");
            }
            var entity = _mapper.Map<LoginLogEntity>(input);
            entity.UserId = user.Id;
            entity.UserName = user.UserName;

            //// 使用 Task 将日志记录操作放到后台执行
            //await TaskExtension.RunAsync(async () =>
            //  {
            //      using (var scope = AppHelper.ServiceProvider.CreateScope())
            //      {
            //          var loginLogRepository = scope.ServiceProvider.GetRequiredService<ILoginLogRepository>();
            //          await loginLogRepository.AddAndSaveAsync(entity);
            //      }
            //  }, "登录日志记录");

            return await _loginLogRepository.AddAndSaveAsync(entity);

            //return true;
        }

        #endregion 添加
    }
}